# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
from typing import Union, List
from pydantic import BaseModel, Field
from fastapi import Query


class ChatHistorySearchIn(BaseModel):
    """ 对话记录搜索参数 """
    page_no: int = Query(default=1, gt=0, description="当前页码")
    page_size: int = Query(default=15, gt=0, le=200, description="每页条数")
    robot: Union[str, None] = Query(default=None, description="智能体名")
    user: Union[str, None] = Query(default=None, description="用户信息")


class ChatHistoryDeleteIn(BaseModel):
    """ 对话记录删除参数 """
    ids: List[int] = Field(default=[])

    class Config:
        json_schema_extra = {
            "example": {
                "ids": [1, 2, 8]
            }
        }


"""--------------- Separator ---------------"""


class ChatHistoryListVo(BaseModel):
    id: int = Field(description="ID")
    robot: str = Field(description="所属智能体")
    scene: str = Field(description="来源场景")
    question: str = Field(description="提问问题")
    answer: str = Field(description="答复内容")
    chat_model: str = Field(description="对话模型")
    use_tokens: int = Field(description="消耗Tokens")
    use_points: int = Field(description="消耗积分")
    ip: str = Field(description="来源IP")
    user_delete: bool = Field(description="用户删除")
    task_time: str = Field(description="执行耗时")
    create_time: str = Field(description="创建时间")
    update_time: str = Field(description="更新时间")
    user: dict = Field(description="用户信息")
    context: list = Field(description="上下文值")
    quotes: list = Field(description="引用内容")
    usages: list = Field(description="使用情况")

    class Config:
        json_schema_extra = {
            "example": {
                "id": 2,
                "robot": "通用智能助手",
                "scene": "test",
                "question": "合同的甲方是谁?",
                "answer": "根据您提供的合同内容...。",
                "chat_model": "GLM-4-Long",
                "use_tokens": 1304,
                "use_points": 0,
                "ip": "127.0.0.1",
                "user_delete": False,
                "task_time": "3.23",
                "create_time": "2025-05-08 10:44:41",
                "update_time": "2025-05-08 10:44:41",
                "user": {
                    "sn": "52778960",
                    "nickname": "罗非鱼",
                    "mobile": "13800138000",
                    "avatar": "http://127.0.0.1:8100/static/images/default_avatar.png"
                },
                "quotes": [
                    {
                        "type": "emb",
                        "uuid": "0d024fbf-15b7-49a1-825a-e9bc9f8f1d2c",
                        "user_id": 0,
                        "know_id": 1,
                        "archive_id": 1,
                        "question": "1． 项目需求变动项导致项目周期变动超过项目...",
                        "answer": "",
                        "emb_score": 0.44203,
                        "file": {
                            "code": "f98fd6aa822be2c08847776eea7764df",
                            "name": "已脱敏合同文件.docx",
                            "size": 22950,
                            "path": "https://xx.com/storage/c264b5de78.docx"
                        }
                    }
                ],
                "context": [
                    {
                        "role": "user",
                        "content": "你好"
                    },
                    {
                        "role": "assistant",
                        "content": "你好！请问有什么可以帮助您的吗？"
                    },
                    {
                        "role": "user",
                        "content": "合同的甲方是谁?"
                    }
                ],
                "usages": [
                    {
                        "module": "知识搜索",
                        "scene": "learning",
                        "alias": "智谱清言",
                        "model": "embedding-3",
                        "task_time": "0.39",
                        "use_points": 0,
                        "total_tokens": 10,
                        "prompt_tokens": 10,
                        "search_mode": "similar",
                        "search_tokens": 5000,
                        "search_similar": 0.4,
                        "ranking_status": False,
                        "optimize_ask": False,
                        "usage_vector": {
                            "use_points": 0,
                            "total_tokens": 10,
                            "prompt_tokens": 10
                        },
                        "usage_rerank": {},
                        "optimize_usage": {}
                    },
                    {
                        "module": "AI 对话",
                        "scene": "chat",
                        "alias": "GLM-4-Long",
                        "model": "glm-4-long",
                        "task_time": "0",
                        "use_points": 0,
                        "total_tokens": 1294,
                        "prompt_tokens": 1218,
                        "completion_tokens": 76
                    }
                ]
            }
        }
